<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 订单管理
 * @author zxj
 *
 */
class Order_new extends MY_Controller {
	private $t_order = 'mt_order';  //订单表
	function __construct() {
		parent::__construct();
        $this->load->model('fee_model','feeModel');
	}


    /**
     * 刷新订单状态
     * @param string $id
     */
    function reloadStatus($id= ''){
        $id = intval($id);
        if($id){
            $this->common_model->resetOrderStatus($id);
        }
        redirect(blang_url('orders/Order_new/inDetails/'.$id));
    }



    /**
     * 批量扣款更新订单金额
     */
    function batchOrder(){
        $sql = "
            select o.id,o.order_num,o.merchant_num,o.grandTotalMerchant,o.currency_code,t.add_time,t.end_sid,c.name,c.CurrencyIsoCode
            from mt_order as o left join mt_order_track as t on o.id = t.oid
            left join mt_customers as c on c.id = o.merchant_num
            where t.end_sid  =  4 and merchant_num > 0 and grandTotalMerchant>0
        ";
        $orderList = $this->common_model->getQuery($sql,'result_array');
        foreach ($orderList as $rs) {
            $records  = $this->common_model->selectFirst('mt_deposit_record',array('orderId'=>$rs['order_num']));
            if($records){
                $this->common_model->delete('mt_deposit_record',array('orderId'=>$rs['order_num']));
            }
            $money = $rs['grandTotalMerchant'];
            if($rs['currency_code'] != $rs['CurrencyIsoCode']){
                $money = currency_service($rs['currency_code'],$rs['CurrencyIsoCode'],$money);
            }
            $inData = array(
                'customers_id'=>$rs['merchant_num'],
                'time'=>$rs['add_time'],
                'orderId'=>$rs['order_num'],
                'money'=>$money,
                'currency'=>$rs['CurrencyIsoCode'],
            );
            $this->common_model->insert('mt_deposit_record',$inData);
        }

    }


    //推送配件采购需求
    function ratePrice(){
        $whereIn =array('id'=>array('848','849','850','851','852','853','854','855','856','857'));
        $partsList = $this->common_model->selectCustom('mt_product_parts','','id asc',$whereIn);
        foreach ($partsList as $rs) {
            gsh_output($rs);
            $newPrice = currency_service($rs['currency'],$rs['rate_currency'],$rs['price']);
            gsh_output($newPrice);
            $this->common_model->update('mt_product_parts',array('rate_price'=>$newPrice),array('id'=>$rs['id']));
            //刷新价格
            $this->common_model->clearProduct($rs['pid']);
            $this->feeModel->setProductPrice($rs['id']);
        }
    }

    /**
     * 推送配件采购需求
     */
    function pushParts(){
        $sql = "select a.*,b.acceptQuotation,b.order_num from mt_product_parts as a
                left join mt_product as b on a.pid = b.id
                where a.is_sync = 0 and a.status = 3 and b.acceptQuotation=1";
        /*
        $sql = "select a.*,b.service_type from mt_product_parts as a
            left join mt_order as b on a.oid = b.id
            where a.is_sync = 0 and a.status = 3 and b.service_type=4";
        */
        $parts = $this->common_model->getQuery($sql,'result_array');
        foreach ($parts as $rs) {
            $this->common_model->createPartsNeed($rs['id']);
        }
    }



    /**
     * 推送报价到网站
     * @param string $id  订单ID
     */
    function pushPrice($id = '')
    {
        $id = intval($id);
        if ($id) {
            $result = $this->common_model->sendStatusWebsite($id);
            var_export($result);
        } else {
            $sql = "select * from mt_order where source_oid  not like 'OM%' and status_id>=3 ORDER BY id desc limit 0,150";
            $orderList = $this->common_model->getQuery($sql,'result_array');
            foreach ($orderList as $rs) {
                $result = $this->common_model->sendStatusWebsite($rs['id']);
                var_export($result);
            }
        }
    }


    /**
     * 更新订单
     * @param string $id
     */
    function reSetOrder($id = ''){
        $this->output->enable_profiler(TRUE);
        $id = intval($id);
        if($id){
            $where = is_numeric($id)?array('id'=>$id):array('order_num'=>$id);
            $orderList = $this->common_model->selectCustom('mt_order',$where,'id asc');
        }else{
            //$sql = 'select * from mt_order where status_id>2 ORDER BY id desc limit 2000,1000';
            $sql = "select * from mt_order where order_num in('wx16083010199575','wx16092110210110','wx16110552525497','wx16122853551001','wx16122951971009','wx16122797515699','wx16122953485055')";
            $orderList = $this->common_model->getQuery($sql,'result_array');
        }
        foreach ($orderList as $key=>$rs) {
            //if($rs['status_id']<5)continue;
            //清除报价
            $this->common_model->clearOrder($rs['id']);
            //产品列表
            $productList = $this->common_model->selectCustom('mt_product',array('oid'=>$rs['id']));
            foreach ($productList as $row) {
                //清除产品报价
                $this->common_model->clearProduct($row['id']);
                //生成产品费用
                $this->feeModel->setProductPrice($row['id']);
            }
        }
    }


    /**
     * 维修列表
     */
    function index()
    {
        //获取表单数据
        $data = $formField = gp();
        //#查询条件
        $searchStr = 'orders/Order_new/index/?';
        $keywordOrder = addslashes(trim($data['keywordOrder']));
        $keywordProduct = addslashes(trim($data['keywordProduct']));
        $pid = addslashes(trim($data['pid']));
        if ($keywordOrder) {
            if (filter_var($keywordOrder, FILTER_VALIDATE_EMAIL)) {
                $where = 'where o.consumer_email = \'' . $keywordOrder . '\' ';
            } else {
                $where = 'where (o.order_num like \'%' . $keywordOrder . '%\' or o.source_oid like \'%' . $keywordOrder . '%\' or o.shipinTrack like \'%' . $keywordOrder . '%\'
                or o.delivery_address like \'%' . $keywordOrder . '%\' or o.consumer_phone like \'%' . $keywordOrder . '%\' or o.consumer_name like \'%' . $keywordOrder . '%\') ';
            }
            $searchStr .= 'keywordOrder=' . $keywordOrder;
        }
        if ($keywordProduct) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= '(p.source_id_order like \'%' . $keywordProduct . '%\' or p.productSku like \'%' . $keywordProduct . '%\' or p.imei like \'%' . $keywordProduct . '%\' or p.cb_qc_imei like \'%' . $keywordProduct . '%\' or p.ck_qc_imei like \'%' . $keywordProduct . '%\') ';
            $searchStr .= '&keywordProduct=' . $keywordProduct;
        }
        //产品ID
        if ($pid) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'p.id = \'' . $pid . '\' ';
            $searchStr .= '&pid=' . $pid;
        }
        //产品状态
        $available = $data['available'];
        if (is_numeric($available)) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'p.available=' . $available;
            $searchStr .= '&available=' . $available;
        }
        //维修状态
        $status_id = intval($data['status_id']);
        if ($status_id) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'p.status_id=' . $status_id;
            $searchStr .= '&status_id=' . $status_id;
        }
        //产品类型
        $category_id = intval($data['category_id']);
        if ($category_id) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'p.category_id=' . $category_id;
            $searchStr .= '&category_id=' . $category_id;
        }
        //服务中心
        $service_center = intval($data['service_center']);
        if ($service_center) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'o.service_center=' . $service_center;
            $searchStr .= '&service_center=' . $service_center;
        }
        //服务类型
        $service_type = intval($data['service_type']);
        if ($service_type) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'o.service_type=' . $service_type;
            $searchStr .= '&service_type=' . $service_type;
        }
        //卖家编号
        $merchant_num = intval($data['merchant_num']);
        if ($merchant_num) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'o.merchant_num=' . $merchant_num;
            $searchStr .= '&merchant_num=' . $merchant_num;
        }
        //品牌编号
        $brandCustomerNum = intval($data['brandCustomerNum']);
        if ($brandCustomerNum) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'p.brandCustomerNum=' . $brandCustomerNum;
            $searchStr .= '&brandCustomerNum=' . $brandCustomerNum;
        }
        //角色
        $role = $this->users['role'];
        $data['role'] = $role;
        $point = $this->users['service_center'];
        if ($role != 'admin' && !$service_center && $point) {
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'o.service_center=' . $point;
        }

        //条件
        if(!$keywordOrder && !$keywordProduct && !$pid){
            $where = $where ? $where . ' and ' : ' where ';
            $where .= 'o.available <> 0 and o.napa_stores_num is NULL';
        }

        //默认select设置
        $optStatus[$status_id] = 'selected';
        $data['optStatus'] = $optStatus;
        $optServiceCenter[$service_center] = 'selected';
        $data['optServiceCenter'] = $optServiceCenter;
        $optCategory[$category_id] = 'selected';
        $data['optCategory'] = $optCategory;
        $optServiceType[$service_type] = 'selected';
        $data['optServiceType'] = $optServiceType;
        $optMerchant[$merchant_num] = 'selected';
        $data['optMerchant'] = $optMerchant;
        $optBrand[$brandCustomerNum] = 'selected';
        $data['optBrand'] = $optBrand;
        $optAvailable[$available] = 'selected';
        $data['optAvailable'] = $optAvailable;
        //分页
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url($searchStr);
        $sql = 'select p.id,p.oid,p.status_id,p.category_id,p.productSku,p.imei,p.brandCustomerNum,p.imei,p.add_user,p.add_time,p.available,p.source_id_order,
                o.order_num,o.source_oid,o.website_origin,o.service_center,o.service_type,o.consumer_email,o.isSync_sf,o.status_id as sId,
                o.consumer_name,o.consumer_phone,o.napa_stores_num,o.delivery_address,o.delivery_country,o.shipoutTrack,
                o.merchant_num,o.create_time,o.shipinTrack,o.available as orderAvailable,o.shippingFeeText,o.product_quantity
                from mt_product p left join mt_order o on p.oid = o.id ' . $where;
        $config['total_rows'] = $this->common_model->getQuery($sql, 'num_rows');
        $config['per_page'] = 20;
        $page = max(1, intval($data['page']));
        $config['uri_segment'] = config_item('enable_lang') ? 5 : 4;
        $offset = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $sql .= ' order by o.create_time desc limit ' . $offset . ',' . $config['per_page'] . ' ';
        $dataList = $this->common_model->getQuery($sql, 'result_array');
        $data['show_pages'] = $this->pagination->create_links();
        //重构数据
        foreach ($dataList as $key => &$rs) {
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $rs['service_type']));
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $rs['category_id']));
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $rs['status_id']));
            $rs['serviceName'] = $serviceType['name_' . $this->curLang];
            $rs['categoryName'] = $categoryFirst['name_' . $this->curLang];
            $rs['statusName'] = $statusFirst['name_' . $this->curLang];
            $rs['add_time'] = 'updateDate:' . date('Y-m-d H:i:s', $rs['add_time']);
            $rs['holdColorOrder'] = $rs['orderAvailable'] == 2?'#ff0909':'#0a8e61';
            $rs['holdColorProduct'] = $rs['available'] == 2?'#ff0909':'#0a8e61';
            if($rs['service_type']==7 && $rs['status_id']==18){
                $rs['nextAct'] = $this->common_model->flowAction($rs['id'], 14);
            }else{
                $rs['nextAct'] = $this->common_model->flowAction($rs['id'], $rs['status_id']);
            }
            //追踪号规则
            $rs['isInTrackNum'] = true;
            if($rs['status_id']==14 && $rs['shipoutTrack']){
                $rs['isInTrackNum'] = false;
            }
            
        }
        unset($rs);
        $data['dataList'] = $dataList;
        //查询服务站点
        //if($role == 'admin'){
            $serviceCenterList = $this->common_model->selectall('mt_service_center');
            $data['serviceCenterList'] = $serviceCenterList;
        //}
        //查询服务类型
        $serviceTypeList = $this->common_model->selectCustom('mt_service_type');
        $data['serviceTypeList'] = $serviceTypeList;
        //查询产品类型
        $categoryList = $this->common_model->selectCustom('mt_product_category');
        $data['categoryList'] = $categoryList;
        //查询所有状态
        $statusList = $this->common_model->selectCustom('mt_product_status');
        $data['statusList'] = $statusList;
        //查询卖家
        $merchantList = $this->common_model->selectCustom('mt_customers',array('type'=>1));
        foreach ($merchantList as &$rs) {
            $rs['name'] = $rs['name_'.$this->curLang]?$rs['name_'.$this->curLang]:$rs['name'];
        }
        unset($rs);
        $data['merchantList'] = $merchantList;
        //查询品牌
        $brandList = $this->common_model->selectCustom('mt_customers',array('type'=>2));
        foreach ($brandList as &$rs) {
            $rs['name'] = $rs['name_'.$this->curLang]?$rs['name_'.$this->curLang]:$rs['name'];
        }
        unset($rs);
        $data['brandList'] = $brandList;
        if($this->debug){
            gsh_output($data);
        }
        //视图以及样式
        $data['curLang'] = $this->curLang;
        $this->load->view('defaults/order_new', $data);
        $this->load->view('defaults/common/footer');
    }


    /**
     * 查看订单详情
     * @param string $id
     * @return mixture
     */
    function inDetails($id='')
    {
        $id = empty($id) ? gp('id') : $id;
        $data['role'] = $this->users['role'];
        //获取订单信息
        $orderFirst = $this->common_model->selectFirst($this->t_order, array('id' => $id));
        $serviceCenter = $this->common_model->selectFirst('mt_service_center', array('id' => $orderFirst['service_center']));
        $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orderFirst['service_type']));
        $statusFirst = $this->common_model->selectFirst('mt_order_status', array('id' => $orderFirst['status_id']));
        $websiteFirst = $this->common_model->selectFirst('mt_order_website', array('id' => $orderFirst['source_site']));
        $orderFirst['serviceCenterName'] = $serviceCenter['name_'.$this->curLang];
        $orderFirst['tax'] = sprintf('%s%%',$serviceCenter['tax']*100);
        $orderFirst['serviceTypeName'] = $serviceType['name_'.$this->curLang];
        $orderFirst['statusName'] = $statusFirst['name_'.$this->curLang];
        $orderFirst['websiteName'] = $websiteFirst['name'];
		 //begin 以下修改订单地址的国家简写为全名 20161125 sqingwen
        $ct_arr = $this->common_model->selectCustom('mt_country');
        $ct = array();
        foreach ($ct_arr as $k => $v) {
            $ct[$v['iso']] = $v['name_en'];
        }
        //如果前两个字母是国家，则只替换前两个字符
        if (($ct[substr($orderFirst['delivery_address'], 0, 2)]) != NULL) {

            $orderFirst['delivery_address'] = $ct[substr($orderFirst['delivery_address'], 0, 2)] . substr($orderFirst['delivery_address'], 2);
        } elseif ($ct[substr($orderFirst['delivery_address'], -2)] != NULL) {
            //否则替换匹配最后两个字符

            $orderFirst['delivery_address'] = substr($orderFirst['delivery_address'], 0, strlen($orderFirst['delivery_address']) - 2) . $ct[substr($orderFirst['delivery_address'], -2)];
        }
        if (($ct[substr($orderFirst['pickup_address'], 0, 2)]) != NULL) {
            $orderFirst['pickup_address'] = $ct[substr($orderFirst['pickup_address'], 0, 2)] . substr($orderFirst['pickup_address'], 2);
        } elseif ($ct[substr($orderFirst['pickup_address'], -2)] != NULL) {
            //否则替换匹配最后两个字符
            $orderFirst['pickup_address'] = substr($orderFirst['pickup_address'], 0, strlen($orderFirst['pickup_address']) - 2) . $ct[substr($orderFirst['pickup_address'], -2)];
        }


	$orderFirst['delivery_country'] = $ct[$orderFirst['delivery_country']];
        //end 以下修改订单地址的国家简写为全名 20161125 sqingwen
		
        if($orderFirst['payment_date']){
            $orderFirst['payment_date'] = date('Y-m-d H:i:s',$orderFirst['payment_date']);
        }
        $consumer_num = $orderFirst['merchant_num'] ? $orderFirst['merchant_num'] : $orderFirst['consumer_num'];
        $consumerFirst = $this->common_model->selectFirst('mt_customers', array('id' => $consumer_num));
        $consumerFirst['name'] = $consumerFirst['name_'.$this->curLang]?$consumerFirst['name_'.$this->curLang]:$consumerFirst['name'];
        $data['consumerFirst'] = $consumerFirst;
        //#产品列表
        $productList = $this->common_model->selectCustom('mt_product', array('oid' => $id));
        foreach ($productList as &$rs) {
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $rs['category_id']));
            $rs['category_name'] = $categoryFirst['name_'.$this->curLang];
            if ($rs['available'] == 2) {
                $rs['status_name'] = '<span style="color: red;">hold</span>';
                continue;
            } else {
                $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $rs['status_id']));
                $rs['status_name'] = $statusFirst['name_'.$this->curLang];
            }
        }
        unset($rs);
        $data['productList'] = $productList;
        //#维修轨迹
        $trackList = $this->common_model->selectCustom('mt_order_track', array('oid' => $id));
        foreach ($trackList as &$rs) {
            $status = $this->common_model->selectFirst('mt_order_status', array('id' => $rs['end_sid']));
            $rs['status_name'] = $status['name_'.$this->curLang];
            $rs['is_sync'] = $rs['is_sync'] ? 'Yes' : 'No';
            $rs['add_time'] = date('Y-m-d H:i:s', $rs['add_time']);
        }
        unset($rs);
        $data['trackList'] = $trackList;
        //hold记录
        $data['holdList'] = $this->common_model->selectCustom('mt_hold_log', array('order_num' => $orderFirst['order_num']));
        //#订单信息
        $orderFirst['add_time'] = date('Y-m-d H:i:s', $orderFirst['add_time']);
        if (isset($orderFirst['delivery_service'])) {
            $orderFirst['delivery_service'] = $orderFirst['delivery_service']
                ? lang('order_kd')
                : lang('order_khzq');
        }
        if (isset($orderFirst['pickup_service'])) {
            $orderFirst['pickup_service'] = $orderFirst['pickup_service']
                ? lang('order_khzf')
                : lang('order_smqj');
        }
        if($orderFirst['storeOrShip']==1){
            $orderFirst['storeOrShip'] = lang('order_fhzt');
        }elseif($orderFirst['storeOrShip']==2){
            $orderFirst['storeOrShip'] = lang('order_dpqj');
        }else{
            $orderFirst['storeOrShip'] = lang('order_rk');
        }

        $data['orderFirst'] = $orderFirst;

        if($this->debug){
            gsh_output($data);
        }
        //views
        $this->breadcrumb->append_crumb(
            array('url' => 'orders/Order_new/inDetails/' . $id, 'name' => lang('order_ddxq'))
        );
        $this->load->view('defaults/order_inDetail_new', $data);
        $this->load->view('defaults/common/footer');
    }



    /**
     * 修改订单
     * @param string $id
     */
    function edit($id = ''){
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $id = intval($id)?intval($id):gp('id');
        $data['id'] = $id;
        $data['role'] = $this->users['role'];
        $orderFirst = $this->common_model->selectFirst($this->t_order,array('id'=>$id));
        if(!$orderFirst){
            show_error('this order not found');
        }
        //判断是否修改数据
        $formData = gp();
        if($formData['submits']){
            $inData = array();
            foreach ($formData as $key=>$val) {
                if(array_key_exists($key,$orderFirst) && $orderFirst[$key] != $val){
                    $inData[$key] = $val;
                }
            }
            if($inData) {
                $inData['add_user'] = $this->users['name'];
                $inData['add_time'] = time();
                if($inData['delivery_country']){
                    $inData['is_pay'] = 0;
                }
                $this->common_model->update($this->t_order, $inData, array('id' => $id));
                //重新计算价格
                if($inData['delivery_country'] && $orderFirst['status_id']>=3){
                    //清除报价
                    $this->common_model->clearOrder($id);
                    //产品列表
                    $productList = $this->common_model->selectCustom('mt_product',array('oid'=>$id));
                    foreach ($productList as $row) {
                        //清除产品报价
                        $this->common_model->clearProduct($row['id']);
                        //生成产品费用
                        $this->feeModel->setProductPrice($row['id']);
                    }
                    //帐号扣款金额
                    $recordPrice = $this->common_model->selectFirst('mt_deposit_record',array('customers_id'=>$orderFirst['merchant_num'],'orderId'=>$orderFirst['order_num']));
                    if($recordPrice && $recordPrice['money']<0){
                        $money=abs($recordPrice['money']);
                        $money_currency=currency_service($orderFirst['currency_code'],'CNY',$money);
                        $inData= array(
                            'customers_id'=>$orderFirst['merchant_num'],
                            'orderId'=>$orderFirst['order_num'],
                            'time'=>time(),'currency'=>'CNY','country_code'=>$orderFirst['point'],
                            'add_name'=>$this->users['name'],'money'=>$money_currency,
                            'transactionId'=>'修改订单发货地址，运费变更,先补回此订单扣款'
                        );
                        $this->common_model->insert('mt_deposit_record',$inData);
                        //更新后的订单信息
                        $orders = $this->common_model->selectFirst('mt_order',array('id'=>$id));
                        if($orders['grandTotalMerchant']>0){
                            $grandTotalMerchant=$orders['grandTotalMerchant'];
                            $merchant_currency=currency_service($orderFirst['currency_code'],'CNY',$grandTotalMerchant);
                            $inData['money'] = '-'.$merchant_currency;
                            $inData['transactionId'] = '修改订单发货地址，运费变更,补回后重新扣款';
                            $this->common_model->insert('mt_deposit_record',$inData);
                        }
                    }

                }

                /*
                if(!$orderFirst['is_pay']){
                    $this->common_model->setOrderPrice($id);
                }
                */
                //hold记录
                if($inData['available']==2){
                    $holdData=array('order_num'=>$orderFirst['order_num'],
                        'remark'=>'手动hold订单','name'=>$this->users['name'],'time'=>time(),
                        'type'=>1,'order_or_product'=>1,'off_or_on'=>1
                    );
                    $this->common_model->insert('mt_hold_log',$holdData);
                }
                //解除hold记录
                if($orderFirst['available']==2 && $inData['available']==1){
                    $holdData=array('order_num'=>$orderFirst['order_num'],
                        'remark'=>'手动解除hold','name'=>$this->users['name'],'time'=>time(),
                        'type'=>1,'order_or_product'=>1,'off_or_on'=>2
                    );
                    $this->common_model->insert('mt_hold_log',$holdData);
                }
                //环球易购同步作废订单
                if($orderFirst['merchant_num']==428 && $inData['available']===0){
                    $this->load->library('GlobalEGrow');
                    $globalEGrow=new GlobalEGrow();
                    $data=$globalEGrow->transferVoid($id);
                }
            }
            redirect(blang_url('orders/Order_new/inDetails/'.$id));
        }else {
            $serviceCenter = $this->common_model->selectFirst('mt_service_center', array('id' => $orderFirst['service_center']));
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orderFirst['service_type']));
            $orderFirst['serviceCenter_name'] = $serviceCenter['name_'.$this->curLang];
            $orderFirst['serviceType_name'] = $serviceType['name_'.$this->curLang];
            $data['orderFirst'] = $orderFirst;
            //获取状态
            $statusList = $this->common_model->selectCustom('mt_order_status');
            $data['statusList'] = $statusList;
            //default
            $ckdAvailable[$orderFirst['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $optStatus[$orderFirst['status_id']] = 'selected';
            $data['optStatus'] = $optStatus;
            $ckdDelivery[$orderFirst['delivery_service']] = 'checked';
            $data['ckdDelivery'] = $ckdDelivery;
            $ckdPickup[$orderFirst['pickup_service']] = 'checked';
            $data['ckdPickup'] = $ckdPickup;
            $ckdPackage[$orderFirst['is_package']] = 'checked';
            $data['ckdPackage'] = $ckdPackage;

            //运费规则 2016-08-20
            $data['isInFreight'] = $this->common_model->isInFreight($orderFirst['id']);
            //加盟商列表
            $data['jList'] = $this->common_model->selectCustom('mt_customers',array('type'=>4));
            //获取国家
            $data['countryList'] = $this->common_model->selectCustom('mt_country',array('available'=>1),'iso asc');
            $optDeliveryCountry[$orderFirst['delivery_country']] = 'selected';
            $data['optDeliveryCountry'] = $optDeliveryCountry;
            //views
            $data['curLang'] = $this->curLang;
            $this->breadcrumb->append_crumb(
                array('url' => 'orders/Order_new/inDetails/'.$id, 'name' => lang('order_ddxq'))
            );
            $this->load->view('defaults/order_post_new',$data);
            $this->load->view('defaults/common/footer');
        }
    }



    /**
     * 删除订单
     * @param string $id
     * @param string $page
     */
    function del($id = '',$page = ''){
        if($this->users['role'] != 'admin' && $this->users['account'] != 'zlj')
            show_error('admin role');
        $id = intval($id);
        if($id){
            //clear product
            $where = array('oid'=>$id);
            $this->common_model->delete('mt_product_log',$where);
            $this->common_model->delete('mt_product_parts',$where);
            $this->common_model->delete('mt_product_track',$where);
            $this->common_model->delete('mt_product',$where);
            $this->common_model->delete('mt_order_log',$where);
            $this->common_model->delete('mt_order_track',$where);
            //clear order
            $where = array('id'=>$id);
            $this->common_model->delete('mt_order',$where);
        }else {
            $orderList = $this->common_model->selectCustom('mt_order', array('available' => 0));
            foreach ($orderList as $rs) {
                $id = intval($rs['id']);
                //clear product
                $where = array('oid' => $id);
                $this->common_model->delete('mt_product_log', $where);
                $this->common_model->delete('mt_product_parts', $where);
                $this->common_model->delete('mt_product_track', $where);
                $this->common_model->delete('mt_product', $where);
                $this->common_model->delete('mt_order_log', $where);
                $this->common_model->delete('mt_order_track', $where);
                //clear order
                $where = array('id' => $id);
                $this->common_model->delete('mt_order', $where);
            }
        }
        redirect(blang_url('orders/Order_new/index/?page='.intval($page)));
    }


    /**
     * 刷新订单价格
     * @param string $id
     * @return bool
     */
    function reloadPrice($id = ''){
        $id = intval($id);
        //$this->common_model->setOrderPrice($id);
        $this->feeModel->setOrderPrice($id);
        if(!$this->debug){
            redirect(blang_url('orders/Order_new/inDetails/' . $id));
        }
        return true;
    }


    /**
     * 推送订单到SF
     * @param string $id
     */
    function pushSF($id = ''){
        $this->load->model('salesforce_model', 'sfModel');
        $result = $this->sfModel->oms_to_sf001($id);
        if ($result['status']) {
            $this->common_model->update('mt_order', array('isSync_sf' => 1), array('id' => $id));
        }
        if(is_array($result)){
            gsh_output($result);
        }else{
            echo $result;
        }
    }


     function ajaxShipping4px()
    {
        $gp = gp();
        $this->load->model('px4_model' , 'px4');
        $id = $gp['id'];
        $result = $this->px4->doShipping($gp['order_num']);

        if ($result['success'] == 'success') {
            $productFirst = $this->common_model->selectFirst('mt_product' , array('id' => $id));
            $status_id = $productFirst['status_id'];
            //#更新状态
            if ($status_id == 14 || $status_id == 18) {
                $this->common_model->resetStatus($id);
            }
        }
        echo(json_encode($result));
        exit;

    }


    function ajaxDoCode()
    {
        $gp = gp();
        $this->load->library('barcode');
        $type = "C128";
        $w = '3';
        $h = '55';
        //$code = $this->barcode->getBarcodeHTML($gp['order_code'] , $type);
        $code = '<div><img style="" src="data:image/png;base64,' . $this->barcode->getBarcodePNG($gp['order_code'] , $type,2,65  ) . '"/><br>' . $gp['order_code'] . '<br/><img  src="data:image/png;base64,' . $this->barcode->getBarcodePNG('TOMREPAIRAPI001' , $type,2,65 ) . '"/><br>TOMREPAIRAPI001<br>' . $gp['order_num'].'</div>';
        echo($code);
        die;
    }

}
